package com.ykh.mybbsdemo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ykh.mybbsdemo.dao.mapper.SysUserMapper;
import com.ykh.mybbsdemo.dao.pojo.dto.PageParams;
import com.ykh.mybbsdemo.dao.pojo.po.SysUser;
import com.ykh.mybbsdemo.service.LoginService;
import com.ykh.mybbsdemo.service.SysUserService;
import com.ykh.mybbsdemo.dao.pojo.vo.ErrorCode;
import com.ykh.mybbsdemo.dao.pojo.vo.LoginUserVo;
import com.ykh.mybbsdemo.dao.pojo.vo.Result;
import com.ykh.mybbsdemo.dao.pojo.vo.UserVo;
import com.ykh.mybbsdemo.dao.pojo.dto.UpdateUserPaaram;
import com.ykh.mybbsdemo.service.VideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-08-30
 * Time: 10:18
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    LoginService loginService;

//    @Autowired
//    VideoService videoService;

    //根据评论人员的ID查询 作者的信息,并返回
    @Override
    public UserVo findUserVoById(Long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        //防止出现空指针
        if( sysUser ==null) {
            sysUser = new SysUser();
            sysUser.setNickname("");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser,userVo);
        return userVo;
    }

    @Override
    public SysUser findUserById(long id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        //防止出现空指针
        if( sysUser ==null) {
            sysUser = new SysUser();
            sysUser.setNickname("杨克鸿");
        }
        return sysUser;
    }

    //登录时,验证用户名和密码
    @Override
    public SysUser findUser(String account, String password) {
       LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(SysUser::getAccount,account);
       queryWrapper.eq(SysUser::getPassword,password);
       queryWrapper.select(SysUser::getAccount,SysUser::getId,SysUser::getAvatar,SysUser::getNickname);
       //只需要一条
       queryWrapper.last("limit 1");
       return sysUserMapper.selectOne(queryWrapper);
    }

    //根据token获取用户信息
    @Override
    public Result findUserInfoByToken(String token) {
        /**
         * 1. 验证token是否合法
         *    是否为空, 解析是否成功 redis是否存在
         * 2. 如果校验失败 返回错误
         * 3. 如果成功, 返回对应的结果 LoginUserVo
         */
        //1 / 2 . 解析是否成功,成功就返回用户信息
        SysUser sysUser = loginService.checkToken(token);
        if( sysUser==null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId, sysUser.getId());
        SysUser sysUser1 = sysUserMapper.selectOne(queryWrapper);
        //走到这里说明校验成功, 返回对应的需要的 loginUserVo 结果
//        //创建loginUserVo 对象
        LoginUserVo loginUserVo = new LoginUserVo();
//        //赋值
        loginUserVo.setId(String.valueOf(sysUser1.getId()));
        loginUserVo.setNickname(sysUser1.getNickname());
        loginUserVo.setAccount(sysUser1.getAccount());
        loginUserVo.setAvatar(sysUser1.getAvatar());
        loginUserVo.setEmail(sysUser1.getEmail());
        loginUserVo.setMobilePhoneNumber(sysUser1.getMobilePhoneNumber());
        loginUserVo.setFansNum(sysUser1.getFansNum());
        loginUserVo.setFocusNum(sysUser1.getFocusNum());
        return Result.success(loginUserVo);

    }

    //根据前端传来的账号查找账户
    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getAccount,account);
        queryWrapper.last("limit 1");
        return this.sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        //保存用户 id会自动生成 , 默认生成的id是分布式id
        this.sysUserMapper.insert(sysUser);
    }

    /**
     * 修改用户信息
     * @param
     * @return
     */
    @Override
    public Result updateUserInfo(UpdateUserPaaram updateUserPaaram) {
        LambdaQueryWrapper<SysUser>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId, updateUserPaaram.getId());
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        sysUser.setNickname(updateUserPaaram.getNickname());
        sysUser.setEmail(updateUserPaaram.getEmail());
        sysUser.setMobilePhoneNumber(updateUserPaaram.getMobilePhoneNumber());
        sysUserMapper.update(sysUser,queryWrapper);
        return Result.success("修改成功");
    }

}
